package lily.com.app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class SQLiteManager {
	private static String DB_NAME = "local";
	private static SQLiteDatabase db = null;
	private static boolean isOpen = false;
	

	private SQLiteManager() {
	}

	public static boolean open(Context context) {
		try {
			db = context.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE,
					null);
		} catch (SQLiteException e) {
			isOpen = false;
//			Log.e("DEBUG", "error in open database: " + DB_NAME);
			e.printStackTrace();
		}
		if (null == db)
			isOpen = false;
		else
			isOpen = true;
		return isOpen;
	}

	public static boolean isOpen() {
		return isOpen;
	}

	public static void close() {
		db.close();
		isOpen = false;
	}
	
	//execute, not select/insert/update/delete
	public static boolean execute(String sql){
		if(!isOpen){
//			Log.e("DEBUG", "database hasn't been opened: " + DB_NAME);
			return false;
		}
		
		try{
			db.execSQL(sql);
		}catch(SQLiteException e){
//			Log.e("DEBUG", "error in execute operation for database");
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	

	//select 
	public static Cursor query(String table, String[] columns, String selection,
            String[] selectionArgs, String groupBy, String having,
            String orderBy){
		if(!isOpen){
//			Log.e("DEBUG", "database hasn't been opened: " + DB_NAME);
			return null;
		}
		
		Cursor cursor = null;
		try{
			cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
		}catch(SQLiteException e){
//			Log.e("DEBUG", "error in query operation for database");
			e.printStackTrace();
			cursor = null;
			return null;
		}
		
		return cursor;
	}
	
	//insert
	public static long insert(String table, String nullColumnHack, ContentValues values){
		if(!isOpen){
//			Log.e("DEBUG", "database hasn't been opened: " + DB_NAME);
			return -1;
		}
		
		long rtn = -1;
		try{
			rtn = db.insert(table, nullColumnHack, values);
		}catch(SQLiteException e){
//			Log.e("DEBUG", "error in insert operation for database");
			e.printStackTrace();
			return -1;
		}
		
		return rtn;
	}
	
	//update 
	public static int update(String table, ContentValues values, String whereClause, String[] whereArgs){
		if(!isOpen){
//			Log.e("DEBUG", "database hasn't been opened: " + DB_NAME);
			return -1;
		}
		
		int rtn = -1;
		try{
			rtn = db.update(table, values, whereClause, whereArgs);
		}catch(SQLiteException e){
//			Log.e("DEBUG", "error in update operation for database");
			e.printStackTrace();
			return -1;
		}
		
		return rtn;
	}
	
	//delete
	public static int delete(String table, String whereClause, String[] whereArgs){
		if(!isOpen){
//			Log.e("DEBUG", "database hasn't been opened: " + DB_NAME);
			return -1;
		}
		
		int rtn = -1;
		try{
			rtn = db.delete(table, whereClause, whereArgs);
		}catch(SQLiteException e){
//			Log.e("DEBUG", "error in delete operation for database");
			e.printStackTrace();
			return -1;
		}
		
		return rtn;
	}
}
